package com.atguigu.zookeeper;


import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.xml.stream.FactoryConfigurationError;
import java.io.IOException;
import java.util.List;

/**
 * 客户端国际通用步骤
 * 1. 创建客户端对象
 * 2. 使用客户端对象进行一些操作
 * 3. 关闭客户端对象
 */
public class zkCli {

    private ZooKeeper zkCli;

    @Before
    public void init() throws IOException, InterruptedException {
        //参数解读
        /**
         * 1.String connectString 连接字符串 hadoop102:2181,hadoop103:2181,hadoop104:2181
         * 2.int sessionTimeout 超时时长
         * 3.监听器
         */
        //1.创建对象
        String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
        int sessionTimeout=5000;
        zkCli = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
            }
        });


    }
    @After
    public void close() throws InterruptedException {
        zkCli.close();
    }

    //获取子节点的列表,并且不监听
    @Test
    public void ls() throws KeeperException, InterruptedException {
        List<String> children = zkCli.getChildren("/", false);
        System.out.println(children);
    }
    //获取子节点的列表,并且监听
    @Test
    public void lsAndWatch() throws KeeperException, InterruptedException {
        List<String> children = zkCli.getChildren("/xiyouji", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event);
                System.out.println("0325 is best of atguigu");
            }
        });
        System.out.println(children);
        Thread.sleep(Long.MAX_VALUE);
    }
    //获取子节点的列表,并且循环监听
    public void lsAndWatch(String path) throws KeeperException, InterruptedException {
        List<String> children = zkCli.getChildren(path, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event);
                System.out.println("0325 is best of atguigu");
                try {
                    lsAndWatch(path);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(children);
    }
    @Test
    public void testWatch() throws KeeperException, InterruptedException {
        lsAndWatch("/xiyouji");
        Thread.sleep(Long.MAX_VALUE);
    }
    @Test
    public void create() throws KeeperException, InterruptedException {
         //zkCli.create("/atguigu","sgg".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
         //zkCli.create("/atguigu","sgg".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT_SEQUENTIAL);
         //zkCli.create("/xiyouji/liushahe","xiawujing".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
         zkCli.create("/xiyouji/liushahe","xiawujing".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
         Thread.sleep(Long.MAX_VALUE);
    }
    //获取节点的值，并且不监听
    @Test
    public void get() throws KeeperException, InterruptedException {
        //如果写的糙一点
        //byte[] data = zkCli.getData("/xiyouji", false, null);
        //System.out.println(new String( data));
        //如果写的精细一点
        Stat stat = zkCli.exists("/xiyouji", false);
        if (stat==null){
            System.out.println("您所查找的节点不存在");
            return;
        }
        byte[] data = zkCli.getData("/xiyouji", false, stat);
        System.out.println(new String( data));
    }
    //获取节点的值，并且监听
    @Test
    public void getAndWatch() throws KeeperException, InterruptedException {
        Stat stat = zkCli.exists("/xiyouji", false);
        if (stat==null){
            System.out.println("您所查找的节点不存在");
            return;
        }
        byte[] data = zkCli.getData("/xiyouji", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event);
                System.out.println("节点值发生变化，接下来你想怎么做呢");
            }
        }, stat);
        System.out.println(new String(data));
        //为了客户端监听 得睡一会
        Thread.sleep(Long.MAX_VALUE);
    }
    //设置节点的值
    @Test
    public void set() throws KeeperException, InterruptedException {
        //如果写的糙一点
        //zkCli.setData("/xiyouji","zaijian".getBytes(),-1);
        //写的精细一点
        Stat stat = zkCli.exists("/xiyouji", false);
        if (stat==null){
            System.out.println("您所查找的节点不存在");
            return;
        }
        zkCli.setData("/xiyouji","woyouhuilaile".getBytes(),stat.getVersion());
    }
    //删除空节点
    @Test
    public void delete() throws KeeperException, InterruptedException {
        Stat stat = zkCli.exists("/xiyouji", false);
        if (stat==null){
            System.out.println("您所查找的节点不存在");
            return;
        }
        zkCli.delete("/xiyouji",stat.getVersion());
    }

    public void deleteAll(String path) throws KeeperException, InterruptedException {
        Stat stat = zkCli.exists(path, false);
        if (stat==null){
            System.out.println("您所查找的节点不存在");
            return;
        }
        //获取子节点
        List<String> children = zkCli.getChildren(path, false);
        //遍历字节递归删除
        for (String child : children) {
            deleteAll(path+"/"+child);
        }
        zkCli.delete(path,stat.getVersion());
    }
    //哪一个方法进行测试
    @Test
    public void testDelete() throws KeeperException, InterruptedException {
        deleteAll("/a");
    }
}
